﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;

namespace Sprotar
{
    public partial class MainWindow : Form
    {
        private ArrayList m_arrBed = new ArrayList();
        private ArrayList m_arrPlant = new ArrayList();

        private int m_plantedQt;
        private int m_harvestedQt;
        private int numberOfCarrotBeds = 0;

        public MainWindow()
        {
            InitializeComponent();

            //Opnum skrána sem geymir allar plönturnar sem notandinn hefur búið til.
            Stream stream = File.OpenRead(@"C:\Program Files\Eplin\gardur.txt");
            if (stream != null)
            {
                IFormatter formatter = new SoapFormatter();
                m_arrPlant = (ArrayList)formatter.Deserialize(stream);
                stream.Close();
            }
        }

        private void OnMakeNewPlant(object sender, EventArgs e)
        {
            NewPlantWindow newPlant = new NewPlantWindow();
            DialogResult result = newPlant.ShowDialog();

            //Ef notandinn ýtti á 'Vista beð'
            if (result == DialogResult.OK)
            {
                //Búum til nýja plöntu:
                Plant plant = new Plant();

                //Geymum gögnin sem notandinn sló inn:
                plant.PlantName = newPlant.TextPlantName;
                plant.BedName = newPlant.TextBedName;
                plant.PlantedQt = Int32.Parse(newPlant.TextPlantedQt);
                plant.HarvestedQt = Int32.Parse(newPlant.TextHarvestedQt);

                //Bætum gögnunum í "skrána" okkar.
                m_arrPlant.Add(plant);

                lblPlantName.Text = plant.PlantName;

                //Vistum plöntuna (og m_arrPlant) í skrána 'gardur.txt'.
                Stream stream = File.OpenWrite(@"C:\Program Files\Eplin\gardur.txt");
                if (stream != null)
                {
                    IFormatter formatter = new SoapFormatter();
                    formatter.Serialize(stream, m_arrPlant);
                    stream.Close();
                }
            }
        }


        #region Plöntuvaltakkar
        //kallað er í þetta fall þegar setja á nýja plöntu í kerfið. Engin serialize virkni
        //er til staðar svo hún vistast ekki á milli þess að forritið er lokað og opnað.
        private void OnChoosingPlantA(object sender, EventArgs e)
        {
            foreach (Plant plant in m_arrPlant)
            {
                m_plantedQt = 0;
                m_harvestedQt = 0;
                if (plant.PlantName == "Gulrót")
                {
                    m_plantedQt += plant.PlantedQt;
                    m_harvestedQt += plant.HarvestedQt;
                    numberOfCarrotBeds++;
                    lblMaxHarvestResult.Text = GetMax(plant.PlantName) + " ";
                    lblAvgHarvestResult.Text = GetAvg(plant.PlantName) + " ";
                    //Það þarf að breyta þessu í %. 0.9 er ekki rétt því þetta er nær 90%.
                    lblRatioHarvestResult.Text = GetRatio(plant.PlantName) + "%";
                    lblPlantName.Text = plant.PlantName;
                }
            }
        }

        private void OnChoosingPlantB(object sender, EventArgs e)
        {
            foreach (Plant plant in m_arrPlant)
            {
                m_plantedQt = 0;
                m_harvestedQt = 0;
                if (plant.PlantName == "Rófur")
                {
                    m_plantedQt += plant.PlantedQt;
                    m_harvestedQt += plant.HarvestedQt;
                    numberOfCarrotBeds++;
                    lblMaxHarvestResult.Text = GetMax(plant.PlantName) + " ";
                    lblAvgHarvestResult.Text = GetAvg(plant.PlantName) + " ";
                    //Það þarf að breyta þessu í %. 0.9 er ekki rétt því þetta er nær 90%.
                    lblRatioHarvestResult.Text = GetRatio(plant.PlantName) + "%";
                    lblPlantName.Text = plant.PlantName;
                }
            }
        }

        private void OnChoosingPlantC(object sender, EventArgs e)
        {
            foreach (Plant plant in m_arrPlant)
            {
                m_plantedQt = 0;
                m_harvestedQt = 0;
                if (plant.PlantName == "Spergilkál")
                {
                    m_plantedQt += plant.PlantedQt;
                    m_harvestedQt += plant.HarvestedQt;
                    numberOfCarrotBeds++;
                    lblMaxHarvestResult.Text = GetMax(plant.PlantName) + " ";
                    lblAvgHarvestResult.Text = GetAvg(plant.PlantName) + " ";
                    //Það þarf að breyta þessu í %. 0.9 er ekki rétt því þetta er nær 90%.
                    lblRatioHarvestResult.Text = GetRatio(plant.PlantName) + "%";
                    lblPlantName.Text = plant.PlantName;
                }
            }
        }

        private void OnChoosingPlantD(object sender, EventArgs e)
        {
            foreach (Plant plant in m_arrPlant)
            {
                m_plantedQt = 0;
                m_harvestedQt = 0;
                if (plant.PlantName == "Grænkál")
                {
                    m_plantedQt += plant.PlantedQt;
                    m_harvestedQt += plant.HarvestedQt;
                    numberOfCarrotBeds++;
                    lblMaxHarvestResult.Text = GetMax(plant.PlantName) + " ";
                    lblAvgHarvestResult.Text = GetAvg(plant.PlantName) + " ";
                    //Það þarf að breyta þessu í %. 0.9 er ekki rétt því þetta er nær 90%.
                    lblRatioHarvestResult.Text = GetRatio(plant.PlantName) + "%";
                    lblPlantName.Text = plant.PlantName;
                }
            }
        }
        #endregion

        #region Tölfræðiföllin GetMax, GetAvg og GetRatio
        //Það er til unit test fyrir þetta fall.
        public double GetMax(string chosenPlant)
        {
            double maxNumber = 0;
            foreach (Plant plant in m_arrPlant)
            {
                if (plant.PlantName == chosenPlant)
                {
                    if (plant.HarvestedQt > maxNumber)
                    {
                        maxNumber = plant.HarvestedQt;
                    }
                }
            }
            return maxNumber;
        }

        //Það er til unit test fyrir þetta fall
        public double GetAvg(string chosenPlant)
        {
            double avgNumber = 0;
            int counter = 0;
            foreach (Plant plant in m_arrPlant)
            {
                if (plant.PlantName == chosenPlant)
                {
                    avgNumber += plant.HarvestedQt;
                    counter++;
                }
            }
            return (avgNumber / counter);
        }

        //Það er til unit test fyrir þetta fall.
        public double GetRatio(string chosenPlant)
        {
            double harvested = 0;
            double planted = 0;
            double ratioNumber = 0;
            foreach (Plant plant in m_arrPlant)
            {
                if (plant.PlantName == chosenPlant)
                {
                    harvested += plant.HarvestedQt;
                    planted += plant.PlantedQt;
                }
            }
            return (ratioNumber = (harvested / planted) * 100);
        }
        #endregion

        private void OnExit(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
